package LeetCode刷题;

/**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2022-01-22 10:56
 * @description: Functions of this class is
 **/
public class 和为target的两个数字 {
    public int[] twoSum(int[] nums, int target) {
        //双指针解法
        int len=nums.length;
        int left=0;
        int right=len-1;
        while(left<right){
            int leftNum=nums[left];
            int rightNum=nums[right];
            if(leftNum+rightNum==target){
                return new int[]{leftNum,rightNum};
            }
            //判断情况
            if(rightNum>target||leftNum+rightNum>target){
                //如果右指针的数字大于target或者左右指针数字之和大于target，即右指针--
                --right;
            }
            else if(leftNum+rightNum<target){
                //如果左右指针数字之和小于target，则左指针++
                ++left;
            }
        }
        return null;
    }
}